package com.dyness.poi.level;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.dyness.sdsp.domain.SdspCity;
import com.dyness.sdsp.domain.SysMessage;
import com.dyness.sdsp.domain.SysMessageHelp;
import com.dyness.sdsp.service.SdspCityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.dyness.poi.controller.ExcelLoadUtil.loadData;

/**
 * @author haiqing.gao
 * @date 2024-01-05 14:44
 */
@Service
public class CityLevelService {

    @Autowired
    private SdspCityService sdspCityService;


    public List<CityExtend> loadCityExtend() {
        String file = ResourceUtil.getResource("city_2.xlsx").getFile();
        // String address = "C:\\Users\\gaohaiqing\\Downloads\\city_2.xlsx";

        // 过滤出无code进行排除
        List<City> cityList = loadData(file, City.class)
                .stream().filter(city -> StringUtils.isNotEmpty(city.getCode()))
                .collect(Collectors.toList());

        List<City> states = cityList.stream().filter(city -> city.getCategory().equals("state")).collect(Collectors.toList());
        List<City> districts = cityList.stream().filter(city -> city.getCategory().equals("district")).collect(Collectors.toList());


        // 校验前两级
        List<String> statesNames = states.stream().map(state -> state.getName()).collect(Collectors.toList());
        List<String> statesGermans = states.stream().map(state -> state.getGerman()).collect(Collectors.toList());
        for (City city : districts) {
            if (!statesNames.contains(city.getState()) && !statesGermans.contains(city.getState())) {
                System.out.println("上下级错误 district = " + city.getName() + "上级, state = " + city.getState());
            }
        }

        // 扩展洲数据
        List<CityExtend> statesExtend = states.stream().map(city -> {
            CityExtend demoExtend = new CityExtend();
            BeanUtil.copyProperties(city, demoExtend);
            demoExtend.setId(IdUtil.getSnowflakeNextId());
            demoExtend.setPid(0L);
            demoExtend.setCategory("state");
            return demoExtend;
        }).collect(Collectors.toList());

        // 扩展地区数据
        List<CityExtend> districtExtend = districts.stream().map(city -> {
            CityExtend demoExtend = new CityExtend();
            BeanUtil.copyProperties(city, demoExtend);
            demoExtend.setId(IdUtil.getSnowflakeNextId());
            Long pid = statesExtend.stream()
                    .filter(state -> state.getName().equals(city.getState()) || state.getGerman().equals(city.getState())
                    )
                    .map(CityExtend::getId)
                    .findFirst().orElse(0L);
            demoExtend.setPid(pid);
            demoExtend.setCategory("district");
            return demoExtend;
        }).collect(Collectors.toList());

        // 校验
        districtExtend.stream()
                .filter(district -> district.getPid() == 0)
                .forEach(district -> System.out.println("地区出错的有district = " + district));

        districtExtend.addAll(statesExtend);
        System.out.println("JSONUtil.toJsonStr(districtExtend) = " + JSONUtil.toJsonStr(districtExtend));
        return districtExtend;
    }

    public void saveLevelData(List<CityExtend> list) {
        List<SysMessage> sysMessageList = new ArrayList<>();
        List<SdspCity> sdspCityList = new ArrayList<>();
        list.forEach(data -> {
            SdspCity cityPO = new SdspCity();
            BeanUtils.copyProperties(data, cityPO);
            String category = cityPO.getPid() == 0 ? "state" : "district";
            cityPO.setCategory(category);
            cityPO.setCreatedBy("2");
            cityPO.setCreatedTime(new Date());
            cityPO.setModifiedBy("2");
            cityPO.setModifiedTime(new Date());
            sdspCityList.add(cityPO);

            // 构造message
            String code = data.getCode();
            String chinese = data.getChinese();
            String english = data.getEnglish();
            String german = data.getGerman();
            SysMessage temp1 = SysMessageHelp.buildZh(code, chinese);
            SysMessage temp2 = SysMessageHelp.buildEn(code, english);
            SysMessage temp3 = SysMessageHelp.buildDe(code, german);
            sysMessageList.add(temp1);
            sysMessageList.add(temp2);
            sysMessageList.add(temp3);
        });

        sdspCityService.saveData(sysMessageList, sdspCityList);
    }
}
